package com.kk.xhr.model.service.impl;

import com.kk.xhr.model.dao.NoteDynamicSqlSupport;
import com.kk.xhr.model.dao.NoteMapper;
import com.kk.xhr.model.entity.Note;
import com.kk.xhr.model.service.INoteService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

/**
 * 笔记服务接口实现
 *
 * @author kkmystery
 * @version 1.0 2021/3/20
 * @since 1.0.0
 */
@Repository
@RequiredArgsConstructor
public class NoteServiceImpl implements INoteService {
    private final NoteMapper noteMapper;

    @Override
    public List<Note> query(Long userId) {
        return noteMapper.select(dsl -> dsl
            .where(NoteDynamicSqlSupport.userId, isEqualTo(userId))
            .orderBy(NoteDynamicSqlSupport.updated.descending()));
    }

    @Override
    public long add(Note Note) {
        noteMapper.insertSelective(Note);
        return Note.getId();
    }

    @Override
    public boolean modify(Note note) {
        if (null == note.getId()) {
            return false;
        }
        return 1 == noteMapper.updateByPrimaryKeySelective(note);
    }

    @Override
    public boolean remove(long id) {
        return 1 == noteMapper.deleteByPrimaryKey(id);
    }
}
